﻿
﻿//=================================Gmap===================================//

function initMap($mapid) {

    //Load google map
    var themapId = $mapid.find("#theMap").selector;
    var themap = new GMap2($(themapId).get(0));
       
    // handle click 
    $($mapid.selector + " .addSt ul li").click(function(){
    loadmap($(this), themapId);
    });
       
}
function loadmap($map, themap) {

        var location = $map.html();

        var lng = $map.attr("longtitude");

        var lat = $map.attr("latitude");

        $('.addSt ul li').removeClass('active');

        $map.addClass('active');

        if (lng && lat) {

            var point = new GLatLng(lat, lng);

            showPoint(point, location, themap);

        }

        else

            showAddress(location, themap);

    }
function showAddress(address, themap) {

    var map = new GMap2($(themap).get(0));

    map.addControl(new GLargeMapControl());

    map.addControl(new GMapTypeControl());

    geocoder = new GClientGeocoder();

    // get GLatLng point for the given address

    geocoder.getLatLng(

                        address,   // address to look for

                        function(point) {  // call back function to process the result                             

                            if (!point) {  // if address not found

                                alert("Sorry the given address [" + address + "] was not found!");

                            }

                            else {

                                //  point contains the required Latitude and Longitude

                                map.setCenter(point, 15);  // point map to the given 

                                var marker = new GMarker(point);

                                map.addOverlay(marker);   // display a marker at the point in the map

                                marker.openInfoWindow(address);  // show address in a window

                            }

                        }

                    );

}

function showPoint(point, address, themap) {

    var map = new GMap2($(themap).get(0));

    map.addControl(new GLargeMapControl());

    map.addControl(new GMapTypeControl());

    //  point contains the required Latitude and Longitude

    map.setCenter(point, 15);  // point map to the given 

    var marker = new GMarker(point, { draggable: true });

    map.addOverlay(marker);   // display a marker at the point in the map

    if (address)

        marker.openInfoWindow(address);  // show address in a window                        

    GEvent.addListener(marker, "dragend", function() {

        var point = marker.getPoint();

        map.panTo(point);

    });

}
